using System;
using System.IO;
using System.Net;
using System.Xml;

namespace winsw
{
	public class Download
	{
		public readonly string From;

		public readonly string To;

		internal Download(XmlNode n)
		{
			From = Environment.ExpandEnvironmentVariables(n.Attributes["from"].Value);
			To = Environment.ExpandEnvironmentVariables(n.Attributes["to"].Value);
		}

		public void Perform()
		{
			WebRequest webRequest = WebRequest.Create(From);
			WebResponse response = webRequest.GetResponse();
			FileStream o = new FileStream(To + ".tmp", FileMode.Create);
			CopyStream(response.GetResponseStream(), o);
			if (File.Exists(To))
			{
				File.Delete(To);
			}
			File.Move(To + ".tmp", To);
		}

		private static void CopyStream(Stream i, Stream o)
		{
			byte[] array = new byte[8192];
			while (true)
			{
				bool flag = true;
				int num = i.Read(array, 0, array.Length);
				if (num <= 0)
				{
					break;
				}
				o.Write(array, 0, num);
			}
			i.Close();
			o.Close();
		}
	}
}
